package com.lanyam.travel.base.controller;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lanyam.travel.base.entity.ProductTicket;
import com.lanyam.travel.base.entity.TicketType;
import com.lanyam.travel.base.service.ITicketTypeService;
import com.lanyam.travel.base.utils.JSONResult;

@RestController
@RequestMapping("/ticket")
public class TicketTypeController {

	@Autowired
	private ITicketTypeService ticketTypeService;
	
	/**
	 * 获取数据库中已经维护的所有票的种类
	 * @return
	 */
	@GetMapping("/getAllTicketType")
	public JSONResult getAllTicketType()
	{
		List<TicketType> allTicketType = ticketTypeService.getAllTicketType();
		return JSONResult.ok(allTicketType);
	}
	/**
	 * 获取当前商品维护的票的种类
	 * @param productId
	 * @return
	 */
	@PostMapping("/getTickeTypeByProductId")
	public JSONResult getTickeTypeByProductId(String productId)
	{
		try {
			if (StringUtils.isEmpty(productId)) {
				return JSONResult.errorMsg("参数不全");
			}
			List<ProductTicket> productTicketList = ticketTypeService.getTickeTypeByProductId(productId);
			return JSONResult.ok(productTicketList);
		}catch (Exception e){
			return JSONResult.errorMsg(e.getMessage());
		}
	}
	
	/**
	 * 增加某个商品对应的票种
	 * @param ticketType
	 * @return
	 */
	@PostMapping("/createTicketType")
	public JSONResult createTicketType(ProductTicket productTicket)
	{
		if(StringUtils.isEmpty(productTicket.getProductId()))
		{
			return JSONResult.errorMsg("参数不全");
		}
		String id = UUID.randomUUID().toString().replace("-", "");
		productTicket.setId(id);
		ticketTypeService.createProductTicket(productTicket);
		return JSONResult.ok(productTicket);
	}
	
	/**
	 * 更新票种信息
	 * @param ticketType
	 * @return
	 */
	@PostMapping("/updateTicketType")
	public JSONResult updateTicketType(ProductTicket productTicket)
	{
		ticketTypeService.updateProductTicket(productTicket);
		return JSONResult.ok();
	}
	
	/**
	 * 删除某个票种信息
	 * @param id
	 * @return
	 */
	@PostMapping("/deleteTicketTypeById")
	public JSONResult deleteTicketTypeById(String id)
	{
		if(StringUtils.isEmpty(id))
		{
			return JSONResult.errorMsg("参数不全");
		}
		ticketTypeService.deleteProductTicketById(id);
		return JSONResult.ok();
	}
	
	/**
	 * 删除某个商品下维护的所有票种
	 * @param productId
	 * @return
	 */
	@PostMapping("/deleteTickeTypeByProductId")
	public JSONResult deleteTickeTypeByProductId(String productId)
	{
		if(StringUtils.isEmpty(productId))
		{
			return JSONResult.errorMsg("参数不全");
		}
		ticketTypeService.deleteProductTicketByProductId(productId);
		return JSONResult.ok();
	}
	/**
	 * 根据id获取票种
	 * @param id
	 * @return
	 */
	@PostMapping("/getTicketTypeById")
	public JSONResult getTicketTypeById(String id)
	{
		if(StringUtils.isEmpty(id))
		{
			return JSONResult.errorMsg("参数不全");
		}
		ProductTicket productTicket = ticketTypeService.getProductTicketById(id);
		return JSONResult.ok(productTicket);
	}
}
